<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="书名" prop="bookName">
        <el-input
          v-model="queryParams.bookName"
          placeholder="请输入书名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图书类别" prop="categoryName" label-width="90px">
<!--        <el-select v-model="form.categoryName" placeholder="图书类别" clearable>-->
<!--          <el-option-->
<!--            v-for="item in categoryOptions"-->
<!--            :key="item.value"-->
<!--            :label="item.label"-->
<!--            :value="item.value"-->
<!--          />-->
<!--        </el-select>-->

        <el-input
          v-model="queryParams.categoryName"
          placeholder="请输入图书类别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作者" prop="bookAuthor">
        <el-input
          v-model="queryParams.bookAuthor"
          placeholder="请输入作者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出版社" prop="publisher">
        <el-input
          v-model="queryParams.publisher"
          placeholder="请输入出版社"
          clearable
          @keyup.enter.native="handleQuery"
        />
      <!--</el-form-item>
      <el-form-item label="原价" prop="orPrice">
        <el-input
          v-model="queryParams.orPrice"
          placeholder="请输入原价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="现价" prop="rePrice">
        <el-input
          v-model="queryParams.rePrice"
          placeholder="请输入现价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="简介" prop="brIntroduction">
        <el-input
          v-model="queryParams.brIntroduction"
          placeholder="请输入简介"
          clearable
          @keyup.enter.native="handleQuery"
        />-->
      </el-form-item>
      <el-form-item label="出版时间" prop="pdTime">
        <el-date-picker clearable
          v-model="queryParams.pdTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出版时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="库存" prop="stock">
        <el-input
          v-model="queryParams.stock"
          placeholder="请输入库存"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="封面" prop="img">
        <el-input
          v-model="queryParams.img"
          placeholder="请输入封面"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否删除" prop="isDel">
        <el-input
          v-model="queryParams.isDel"
          placeholder="请输入是否删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:bookinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:bookinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:bookinfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:bookinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bookinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" align="center" />
      <el-table-column label="书籍id" align="center" prop="bookId" />
      <el-table-column label="封面" align="center" prop="img" width="150">
      <template slot-scope="scope">
        <image-preview :src="scope.row.img" :width="100" :height="100"/>
      </template>
    </el-table-column>
      <el-table-column label="书名" align="center" prop="bookName" />
<!--      <el-table-column label="图书类别id" align="center" prop="categoryId" />-->
      <el-table-column label="图书类别" align="center" prop="categoryName" />
      <el-table-column label="作者" align="center" prop="bookAuthor" />
      <el-table-column label="出版社" align="center" prop="publisher" />
      <el-table-column label="原价" align="center" prop="orPrice" />
      <el-table-column label="现价" align="center" prop="rePrice" />
      <el-table-column label="简介" align="center" prop="brIntroduction" />
      <el-table-column label="出版时间" align="center" prop="pdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="库存" align="center" prop="stock" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:bookinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:bookinfo:remove']"
          >删除</el-button>
          <!--..待修改-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-shopping-cart-full"
            @click="handleAddCart(scope.row)"
            v-hasPermi="['system:shopcart:add']"
          >加入购物车</el-button>
          <!--..待修改-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-sell"
            @click="handleAddOrder(scope.row)"
            v-hasPermi="['system:order:add']"
          >立即购买</el-button>
          <!--点击立即购买后会弹出对话框确认书名并输入要购买的数量，填写完毕后，点击确定可以跳转到订单结算界面。
          或者在对话框获取用户地址并显示在对话框上，直接修改“确定”变为“结算/提交订单”-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改图书信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="书名" prop="bookName">
          <el-input v-model="form.bookName" placeholder="请输入书名" />
        </el-form-item>
        <el-form-item label="图书类别id" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入图书类别Id" />
        </el-form-item>
        <el-form-item label="作者" prop="bookAuthor">
          <el-input v-model="form.bookAuthor" placeholder="请输入作者" />
        </el-form-item>
        <el-form-item label="出版社" prop="publisher">
          <el-input v-model="form.publisher" placeholder="请输入出版社" />
        </el-form-item>
        <el-form-item label="原价" prop="orPrice">
          <el-input v-model="form.orPrice" placeholder="请输入原价" />
        </el-form-item>
        <el-form-item label="现价" prop="rePrice">
          <el-input v-model="form.rePrice" placeholder="请输入现价" />
        </el-form-item>
        <el-form-item label="简介" prop="brIntroduction">
          <el-input v-model="form.brIntroduction" placeholder="请输入简介" />
        </el-form-item>
        <el-form-item label="出版时间" prop="pdTime">
          <el-date-picker clearable
            v-model="form.pdTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出版时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="库存" prop="stock">
          <el-input v-model="form.stock" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="封面" prop="img">
          <image-upload v-model="form.img"/>
        </el-form-item>
<!--        <el-form-item label="是否删除" prop="isDel">-->
<!--          <el-input v-model="form.isDel" placeholder="默认为0(未删除)" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--加入购物车对话框-->
    <el-dialog :title="title" :visible.sync="openCart" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="书名" prop="bookName">
          <el-input v-model="form.bookName" placeholder="请输入书名" :disabled="true"/>
        </el-form-item>
        <el-form-item label="数量" prop="num">
          <el-input v-model="form.num" placeholder="请输入数量" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormCart">确 定</el-button>
        <el-button @click="cancelC">取 消</el-button>
      </div>
    </el-dialog>

    <!--立即购买对话框-->
    <el-dialog :title="title" :rules="rules" :visible.sync="openOrder" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="书名" prop="bookName">
          <el-input v-model="form.bookName" placeholder="请输入书名" :disabled="true"/>
        </el-form-item>
        <el-form-item label="原价" prop="orPrice">
          <el-input v-model="form.orPrice" placeholder="请输入原价" :disabled="true"/>
        </el-form-item>

      <el-form-item label="售价" prop="rePrice">
        <el-input v-model="form.rePrice" placeholder="请输入售价" :disabled="true"/>
      </el-form-item>

      <el-form-item label="手机号" prop="tele">
        <el-input v-model="form.tele" placeholder="请输入手机号码" />
      </el-form-item>

        <el-form-item label="地址" prop="address">
                  <el-select v-model="form.address"  placeholder="请选择地址" clearable>
                    <el-option
                      v-for="item in addressOptions"
                      :key="item.id"
                      :label="item.address"
                      :value="item.id"
                    ></el-option>
                  </el-select>
        </el-form-item>

        <el-form-item label="购买数量" prop="pNum">
            <el-input v-model="form.pNum" placeholder="请输入购买数量" />
        </el-form-item>

        <el-form-item label="总计" prop="payPrice">
          <span>¥{{TotalPrice}}元</span>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormOrder">购买</el-button>
        <el-button @click="cancelO">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listBookinfo, getBookinfo, delBookinfo, addBookinfo, updateBookinfo, listCategory} from "@/api/system/bookinfo";
import {addShopcart} from "@/api/system/shopcart"
import {addOrder} from "@/api/system/order";
import {addGoods} from "@/api/system/goods";
import {listAddress} from "@/api/system/address";
export default {
  name: "Bookinfo",
  data() {
    return {
      byBookNum: 1,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 图书信息表格数据
      bookinfoList: [],

      addressOptions:[],

      //图书类别
      categoryOptions: [{
        value: '计算机',
        label: '计算机'
      }, {
        value: '文学',
        label: '文学'
      }, {
        value: '科幻',
        label: '科幻'
      }],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openCart:false,
      openOrder:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bookName: null,
        categoryName:null,
        categoryId: null,
        bookAuthor: null,
        publisher: null,
        orPrice: null,
        rePrice: null,
        brIntroduction: null,
        pdTime: null,
        stock: null,
        img: null,
        isDel: null,
        bookNum:0
      },
      userid: this.$store.getters.id,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    // 查询图书列表
    getCategory(){
      this.loading = true;
      listCategory().then(response => {
        this.categoryOptions = response.rows;
      });
    },

    /** 查询图书信息列表 */
    getList() {
      this.loading = true;
      listBookinfo(this.queryParams).then(response => {
        this.bookinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelC(){
      this.openCart=false;
      this.reset();
    },
    cancelO(){
      this.openOrder=false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bookId: null,
        bookName: null,
        categoryId: null,
        bookAuthor: null,
        publisher: null,
        orPrice: null,
        rePrice: null,
        brIntroduction: null,
        pdTime: null,
        stock: null,
        img: null,
        isDel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.bookId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加图书信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bookId = row.bookId || this.ids
      getBookinfo(bookId).then(response => {
        //console.log(row.bookId)
        this.form = response.data;
        this.open = true;
        this.title = "修改图书信息";
      });
    },


    /**购物车按钮操作*/
    handleAddCart(row){
      this.reset();
      const bookId = row.bookId || this.ids
      getBookinfo(bookId).then(response => {
        this.form = response.data;
        this.openCart = true;
        this.title = "加入购物车";
      });
    },

    /**立即购买操作*/
    handleAddOrder(row){
      this.reset();
      const bookId = row.bookId
      listAddress().then(response =>{
        this.addressOptions =response.rows;
      })
      getBookinfo(bookId).then(response => {
        this.form = response.data;
        this.openOrder = true;
        this.title = "立即购买";//可修改为确认订单
      });
    },
    /**加入购物车弹窗提交按钮——未修改完毕，传bookid和数量，用代码自动抓取用户id，后端实现传入数据*/
    submitFormCart() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.num > 0){
            if(this.form.num > this.form.stock){
              this.$modal.msgError("货存不足，请重新选择");
            }else{
              if (this.form.bookId != null) {
                  addShopcart(this.form).then(response => {
                  this.$modal.msgSuccess("成功加入购物车");
                  this.openCart = false;
                });
              }}
          }else{
            this.$modal.msgError("您输入的数量不正确，请重新输入");
          }

        }
      });
    },

    /**立即购买提交按钮*/
    submitFormOrder() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.pNum > 0){
            if(this.form.pNum > this.form.stock){
              this.$modal.msgError("货存不足，请重新选择");
            }else{
          if (this.form.bookId != null) {
            var _this = this
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("购买成功，快去看看吧");
              _this.form.stock = _this.form.stock - _this.form.pNum;
                updateBookinfo(_this.form).then(response => {
                  this.getList();
                });
              this.openOrder = false;
            });
          }}
        }else{
            this.$modal.msgError("您输入的数量不正确，请重新输入");
          }}
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.bookId != null) {
            updateBookinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBookinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bookIds = row.bookId || this.ids;
      this.$modal.confirm('是否确认删除图书信息编号为"' + bookIds + '"的数据项？').then(function() {
        return delBookinfo(bookIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/bookinfo/export', {
        ...this.queryParams
      }, `bookinfo_${new Date().getTime()}.xlsx`)
    },

  },
  computed:{
    TotalPrice(){
      let aData = new Date();
      if(this.form.pNum) {
        this.form.payPrice = this.form.pNum * this.form.rePrice;
        this.form.orderTime = aData;
        return this.form.pNum * this.form.rePrice;
      }else{
        return 0
      }
    }
  }
};
</script>
